﻿/*
=======================================================
    StandStatus【状态标准】
=======================================================
*/

namespace Services
{
    public interface IStandStatusLogic : ILogic<StandStatusModel, StandStatusFilterModel>
    {
    }


    public class StandStatusLogic : BaseLogic<StandStatusModel, StandStatusFilterModel, StandStatus, IStandStatusRepository, IStandStatusMapping, IStandStatusService>, IStandStatusLogic
    {
        private readonly IDataDepartmentRepository _IDataDepartmentRepository;
        private readonly IDepartmentRepository _IDepartmentRepository;

        public StandStatusLogic(ICurrentUser currentUser, IStandStatusRepository repository, IStandStatusMapping mapping, IStandStatusService service, IDataDepartmentRepository iDataDepartmentRepository, IDepartmentRepository iDepartmentRepository)
            : base(currentUser, repository, mapping, service)
        {
            _IDataDepartmentRepository = iDataDepartmentRepository;
            _IDepartmentRepository = iDepartmentRepository;
        }

        #region GetPage
        public override PageModel<StandStatusModel> GetPage(StandStatusFilterModel filterModel, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            var pageData = base.GetPage(filterModel, pageIndex, pageSize, orderBy);

            //获取当前用户应该有的数据权限范围
            var (isAll, dataPermissionDepartmentIDs) = _currentUser.CurrentUserInfo.LoginUserRoleDepartment;

            //获取所有部门设置的数据权限
            List<DataDepartment> departmentDataPermissions = _IDataDepartmentRepository.GetDataDepartmentsByDataIDs(DataDepartment_Type.状态标准, pageData.Items.Select(x => x.StandStatusID).ToArray());

            var departmentList = _IDepartmentRepository.GetAll();

            foreach (var item in pageData.Items)
            {
                //获取当前循环这条部门数据的所设置的数据权限
                var _currentDataPermissions = departmentDataPermissions.Where(x => x.DataID == item.StandStatusID).Select(x => x.DepartmentID).ToList();

                //设置每一条数据的数据权限的部门名称
                if (_currentDataPermissions.Any(x => x == Gx._NO_DATA_PERMISSION_VALUE))
                {
                    item.SelectDepartmentNames = new List<string>() { "不限" };
                }
                else
                {
                    item.SelectDepartmentNames = departmentList.Where(x => _currentDataPermissions.Contains(x.DepartmentID)).Select(x => x.Name).ToList();
                }
            }
            return pageData;
        }
        #endregion

        #region Insert
        public override StandStatusModel Insert(StandStatusModel model)
        {
            var res = base.Insert(model);
            _IDataDepartmentRepository.InsertByDataID(DataDepartment_Type.状态标准, res.StandStatusID, model.SelectDepartmentIDs.ToArray());
            return res;
        }
        #endregion

        #region Update
        public override int Update(StandStatusModel model)
        {
            _IDataDepartmentRepository.UpdateByDataID(DataDepartment_Type.状态标准, model.StandStatusID, model.SelectDepartmentIDs.ToArray());
            return base.Update(model);
        }
        #endregion

        #region GetByID
        public override StandStatusModel GetByID(int id)
        {
            var item = base.GetByID(id);
            item.SelectDepartmentIDs = _IDataDepartmentRepository.GetDataDepartmentsByDataIDs(DataDepartment_Type.状态标准, id).Select(x => x.DepartmentID).ToList();
            return item;
        }
        #endregion
    }
}
